#include "ubvm_laser.h"
#include "DIVA_Manager.h"
#include <sstream>
#include "xParameterPack.h"
#include "vmLibrary.h"
// 
//  name: ypc
//  history:  2009/3/10 
//            The original file x_cuboid_int_ptvm is copied to be the tempory body.

//Name           : hyt
//Update Date    : 08/10/23
//Update History :
//				(1) VL_createEntity 必須給 meshname,material可給可不給,若想自行命名vm的開頭,則可以
//                   再加第三個參數 string specialhead
//              (2) VL_regBoudingBox 則需要 vm的entity 
//              (3) 由programmer自行決定node內容
//              (4) 若要在vm任何一個位置顯示文字,首先呼叫VL_createVMText(你想顯示的文字,文字node的開頭name)
//                  再來是呼叫VL_createTextNode(vm文字位置)
//              (5) 設定vm node顏色則呼叫VL_setEntityColor(vm entity , Vector4(colorvalue))來設定

ubvm_laser::ubvm_laser(xParameterPack xpp):DIVA_VM(xpp){
	int defaultport = vmLibrary::instance().get_vm_default_input_parameters().size();
	sx = xpp.paras[defaultport].val_float_ptr;
	sy = xpp.paras[defaultport+1].val_float_ptr;
	sz = xpp.paras[defaultport+2].val_float_ptr;
	ex   = xpp.paras[defaultport+3].val_float_ptr;
	ey   = xpp.paras[defaultport+4].val_float_ptr;
	ez   = xpp.paras[defaultport+5].val_float_ptr;
	//_startVM = xpp.paras[defaultport].val_vm;
	//_endVM = xpp.paras[defaultport+1].val_vm;

	init_default_parameters();
	// VL_addVMAnimation();

	VL_addVMAnimation();
	enableVMAnimation();
}

/// destructor
ubvm_laser::~ubvm_laser(){
}

void ubvm_laser::load_3Dresources() {	
	_track = setupTrailLights(Ogre::Vector3(*sx,*sy,*sz),Ogre::Vector3(*ex,*ey,*ez));///add by James
}
void ubvm_laser::set_default_values() {
	_vm_rx_val = 0 ;
	_vm_ry_val = 0 ;
	_vm_rz_val = 0 ;

	_vm_init_sizex = 1 ;
	_vm_init_sizey = 1 ;
	_vm_init_sizez = 1 ;

	_vm_sizex_val = 1 ;
	_vm_sizey_val = 1 ;
	_vm_sizez_val = 1 ;

	_vm_sizexscale_val = 1.0 ;
	_vm_sizeyscale_val = 1.0 ;
	_vm_sizezscale_val = 1.0 ;

	_vm_r_val = 0.2;
	_vm_g_val = 0.2;
	_vm_b_val = 0.2;
	_vm_transparency_val = 1.0 ;

	_vm_yaw_val = 0 ;
	_vm_pitch_val = 0 ;
	_vm_roll_val = 0 ;

}

void ubvm_laser::reg_event_entity() {
}


void ubvm_laser::VMValueUpdated(){
	DIVA_VM::VMValueUpdated();
	//_track = setupTrailLights(Vector3(*sx,*sy,*sz),Vector3(*ex,*ey,*ez));///add by James
}
void ubvm_laser::setBodyScale(Ogre::Vector3 scale) {

}
bool ubvm_laser::isBodyScaleWrtSceneNode() { return true ; }

void ubvm_laser::VMPicked(Ogre::Real x, Ogre::Real y, Ogre::Real z){

}
void ubvm_laser::VMAnimation(const Ogre::FrameEvent &evt){
	_track->getNodeKeyFrame(0)->setTranslate(Ogre::Vector3(*sx,*sy,*sz));
	_track->getNodeKeyFrame(1)->setTranslate(Ogre::Vector3(*ex,*ey,*ez));

	// update line name position
	if (_name_scene_node)
	{
		Ogre::Real x, y, z;
		x = computeCenter(*sx, *ex);
		y = computeCenter(*sy, *ey);
		z = computeCenter(*sz, *ez);
		_name_scene_node->setPosition(Ogre::Vector3(x,y,z));
	}

	_animState->addTime(evt.timeSinceLastFrame);	
	
}
Ogre::NodeAnimationTrack* ubvm_laser::setupTrailLights(Ogre::Vector3 start,Ogre::Vector3 end)
{
	static int num = 0;
	num++;
	Ogre::String number = intToString(num);
	Ogre::String name = "ShouldNotClicked";
	name = name + number + "ubvm_laser" ; // laser_ubrvm already use name+number
	                                      // to avoid collision, we add extra name 
	Ogre::NameValuePairList pairList;
	pairList["numberOfChains"] = "2";
	pairList["maxElements"] = "80";
	Ogre::RibbonTrail* trail = static_cast<Ogre::RibbonTrail*>(
		DIVA_Manager::getInstance()->getSceneManager()->createMovableObject(name, "RibbonTrail", &pairList));
	trail->setMaterialName("Examples/LightRibbonTrail");
	trail->setTrailLength(4000);


	DIVA_Manager::getInstance()->getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(trail);
	// Create 3 nodes for trail to follow
	Ogre::SceneNode* animNode = DIVA_Manager::getInstance()->getSceneManager()->getRootSceneNode()->createChildSceneNode();

	animNode->setPosition(start);

	Ogre::Animation* anim = DIVA_Manager::getInstance()->getSceneManager()->createAnimation("an"+name , 4);//表示這個動畫是 4 秒
	anim->setInterpolationMode(Ogre::Animation::IM_SPLINE);
	Ogre::NodeAnimationTrack* track = anim->createNodeTrack(1, animNode);

	Ogre::TransformKeyFrame* kf = track->createNodeKeyFrame(0);

	kf->setTranslate(start);

	kf = track->createNodeKeyFrame(4);
	kf->setTranslate(end);


	_animState= DIVA_Manager::getInstance()->getSceneManager()->createAnimationState("an"+name);
	_animState->setLoop(true);
	_animState->setEnabled(true);

	int a=0;
	trail->setInitialColour(0, 1.0, 0.8, 0);
	trail->setColourChange(0, 0.5, 0.5, 0.5, 0.5);
	trail->setInitialWidth(0, 5);
	trail->addNode(animNode);
	//AxisAlignedBox box = trail->getBoundingBox();
	//box.scale(Vector3(0,0,0));
	return track;
}

Ogre::Real ubvm_laser::computeCenter( float start, float end )
{
	Ogre::Real center;
	if ( start < end ){
		center = ( end - start )/2 + start;
	} else {
		center = ( start - end )/2 + end;
	}
	return center;
}
